package com.kapil.framework.lang;


/**
 * Assertion utility class that assists in validating arguments. Useful for identifying programmer errors early and
 * clearly at runtime.
 * 
 * <p>
 * This class is similar to TestNG's assertion library. If an argument value is deemed invalid, an
 * {@link AssertionError} is thrown.
 * 
 */
public abstract class Assert
{

    /**
     * Assert a boolean expression, throwing <code>AssertionError</code> if the test result is
     * <code>false</code>.
     * 
     * <pre class="code">
     * Assert.isTrue(count &gt; 0, &quot;The value should be greater than zero&quot;);
     * </pre>
     * 
     * @param expression a boolean expression
     * @param message the exception message to use if the assertion fails
     * @throws AssertionError if expression is <code>false</code>
     */
    public static void isTrue(boolean expression, String message)
    {
        if (!expression)
        {
            throw new AssertionError(message);
        }
    }


    /**
     * Assert a boolean expression, throwing <code>AssertionError</code> if the test result is
     * <code>false</code>.
     * 
     * <pre class="code">
     * Assert.isTrue(count &gt; 0);
     * </pre>
     * 
     * @param expression a boolean expression
     * @throws AssertionError if expression is <code>false</code>
     */
    public static void isTrue(boolean expression)
    {
        isTrue(expression, "Assertion failed - this expression must be true");
    }


    /**
     * Assert that an object is <code>null</code> .
     * 
     * <pre class="code">
     * Assert.isNull(value, &quot;The value must be null&quot;);
     * </pre>
     * 
     * @param object the object to check
     * @param message the exception message to use if the assertion fails
     * @throws AssertionError if the object is not <code>null</code>
     */
    public static void isNull(Object object, String message)
    {
        if (object != null)
        {
            throw new AssertionError(message);
        }
    }


    /**
     * Assert that an object is <code>null</code> .
     * 
     * <pre class="code">
     * Assert.isNull(value);
     * </pre>
     * 
     * @param object the object to check
     * @throws AssertionError if the object is not <code>null</code>
     */
    public static void isNull(Object object)
    {
        isNull(object, "Assertion failed - the object argument must be null");
    }


    /**
     * Assert that an object is not <code>null</code> .
     * 
     * <pre class="code">
     * Assert.notNull(clazz, &quot;The class must not be null&quot;);
     * </pre>
     * 
     * @param object the object to check
     * @param message the exception message to use if the assertion fails
     * @throws AssertionError if the object is <code>null</code>
     */
    public static void notNull(Object object, String message)
    {
        if (object == null)
        {
            throw new AssertionError(message);
        }
    }


    /**
     * Assert that an object is not <code>null</code> .
     * 
     * <pre class="code">
     * Assert.notNull(clazz);
     * </pre>
     * 
     * @param object the object to check
     * @throws AssertionError if the object is <code>null</code>
     */
    public static void notNull(Object object)
    {
        notNull(object, "Assertion failed - this argument is required; it must not be null");
    }
}
